package cpi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cpi.dao.CpiDao;
import cpi.entity.Cpi;
import cpi.service.CpiService;
import cpi.tools.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (Cpi)表服务实现类
 *
 * @author makejava
 * @since 2022-07-07 11:25:10
 */
@Service("cpiService")
public class CpiServiceImpl extends ServiceImpl<CpiDao, Cpi> implements CpiService {
    @Resource
    private CpiDao cpiDao;
//    @Resource
//    public CpiService cpiService;
    @Override
    public  List<Map<String,Object>> byprovince(int page,int size,String type,int time) {
        List<String> m=new ArrayList<>();
        int year=2022;
        int month=5;
        for(int i=0;i<time;i++){
            String mm=month<10?"0"+month:""+month;
            m.add(year+"-"+mm);
            month--;
            if(month==0){
                year--;
                month=12;
            }
        }
        return cpiDao.byprovince(m,(page-1)*size,size,type);
    }

    @Override
    public List<Map<String, Object>> bytype(String province,int time) {
        List<String> m=new ArrayList<>();
        int year=2022;
        int month=5;
        for(int i=0;i<time;i++){
            String mm=month<10?"0"+month:""+month;
            m.add(year+"-"+mm);
            month--;
            if(month==0){
                year--;
                month=12;
            }
        }
        return cpiDao.bytype(m,province);
    }

    @Override
    public List<String> alltype() {
        return cpiDao.alltype();
    }

    @Override
    public List<String> allprovince() {
        return cpiDao.allprovince();
    }

    @Override
    public List<Cpi> listPage(int page, int size, String type, String province) {
        return cpiDao.listPage((page-1)*size,size,type,province);
    }

    @Override
    public int counts(String type, String province) {
        return cpiDao.counts(type,province);
    }
    @Override
    public CommonResult findCpiData(String province, String date, String type, Integer page, Integer size) {

        // 1: 查询所有的分类信息
        QueryWrapper<Cpi> queryWrapper = new QueryWrapper<>();

        // 2：查询发布的分类
        if(StringUtils.isNotBlank(province)) {
            queryWrapper.eq("province", province);
        }
        if(StringUtils.isNotBlank(date)) {
            StringBuilder time = new StringBuilder(date);
            time.append("-01");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date representTime = null;
            try {
                representTime = sdf.parse(String.valueOf(time));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            queryWrapper.eq("represent_time", representTime);
        }

        if(StringUtils.isNotBlank(type)) {
            queryWrapper.eq("type", type);
        }

        int defalutPage = page != null ? page : 1;
        int defalutSize = size != null ? size : 5;

        HashMap<String, Object> map = new HashMap<>();
        map.put("data", this
                .page(new Page<Cpi>(defalutPage, defalutSize), queryWrapper).getRecords());
        map.put("total",this.count(queryWrapper));

        return CommonResult.success(map);

    }
    @Override
    public boolean add(Cpi cpi) {
        return  cpiDao.add(cpi)>0;
    }
    @Override
    public Map<String, Object> predict(String type, String province,int time) {
        List<String> m=new ArrayList<>();
        int year=2022;
        int month=7;
        for(int i=0;i<time;i++){
            String mm=month<10?"0"+month:""+month;
            m.add(year+"-"+mm);
            month++;
            if(month==13){
                year++;
                month=1;
            }
        }
        return cpiDao.predict(m,type,province);
    }
}

